home *** CD-ROM | disk | FTP | other *** search
/ El Mac 9 / El Mac 9.iso / Shareware / Applications / MathPad 2.4 / XFuns / XFun kit / scatter src / doevent.c next >
Encoding:
C/C++ Source or Header  |  1996-03-07  |  4.0 KB  |  204 lines  |  [TEXT/CWIE]

  1. /*
  2.  This file is a fairly generic event handler for an XFun with a single window.
  3.  It calls DrawPlot() on update and assumes globals myWindow and myCursor have been set.
  4. */
  5.  
  6. #include "callbackg.h"
  7. #include "XFundef.h"
  8.  
  9. static int checkcursor(void)    // called whenever cursor region may have changed
  10. {
  11.     Rect inside;
  12.     RgnHandle inrgn;
  13.     
  14.     if(FrontWindow() != myWindow) return(FALSE);
  15.     
  16.     inside = myWindow->portRect;
  17.     inside.right-=15;
  18.     inside.bottom-=15;    // assume space for scroll bars
  19.     SetPort(myWindow);
  20.     LocalToGlobal(&topLeft(inside));
  21.     LocalToGlobal(&botRight(inside));
  22.      inrgn = NewRgn();
  23.     RectRgn(inrgn,&inside);
  24.     SetCursorRegion(*myCursor,inrgn);
  25.     DisposeRgn(inrgn);
  26.     return(TRUE);
  27. }
  28.  
  29. static int DoMouseDown(EventRecord    *theEvent)
  30. {
  31.     WindowPtr theWindow;
  32.     int    partCode;
  33.     long size;
  34.     Rect sizeLimit;
  35.     GrafPtr oldPort;
  36.  
  37.     partCode = FindWindow(theEvent->where,&theWindow);
  38.     /* to intercept menu events
  39.     if(partCode == inMenuBar)
  40.     {
  41.      selector = MenuSelect(theEvent->where);
  42.      if(mymenu(selector)) return(TRUE);
  43.      DoMenuItem(selector);
  44.      return(FALSE);
  45.     }
  46.     */
  47.      if(theWindow != myWindow) return(FALSE);
  48.      
  49.     // reach here only if MouseDown was in myWindow
  50.     switch(partCode)
  51.       {
  52.       case inDrag:
  53.         SelectWindow(theWindow);
  54.         SetRect(&sizeLimit,0,25,4000,4000);
  55.           DragWindow(theWindow,theEvent->where,&sizeLimit);
  56.           checkcursor();
  57.           break;
  58.             
  59.       case inContent:
  60.         if(theWindow != FrontWindow())
  61.         {
  62.          SelectWindow(theWindow);
  63.          break;
  64.         }
  65.         /* handle click in window
  66.         GetPort(&oldPort);
  67.         SetPort(theWindow);
  68.          ...  
  69.         SetPort(oldPort);
  70.         */
  71.         break;
  72.           
  73.       case inGoAway:
  74.           if(TrackGoAway(theWindow,theEvent->where))
  75.           {
  76.            HideWindow(theWindow);
  77.         }
  78.           break;
  79.  
  80.       case inGrow:
  81.         SetRect(&sizeLimit,150,70,1000,1000);
  82.         size = GrowWindow(theWindow,theEvent->where,&sizeLimit);
  83.         if(size)
  84.         {
  85.          GetPort(&oldPort);
  86.          SetPort(theWindow);
  87.          EraseRect(&theWindow->portRect);
  88.          InvalRect(&theWindow->portRect);
  89.          SizeWindow(theWindow,LoWord(size),HiWord(size),TRUE);
  90.          DrawGrowIcon(theWindow);
  91.          SetPort(oldPort);
  92.         }
  93.         break;
  94.         
  95.       case inZoomIn:
  96.       case inZoomOut:
  97.         if(TrackBox(theWindow,theEvent->where,partCode))
  98.         {
  99.          GetPort(&oldPort);
  100.          SetPort(theWindow);
  101.          ZoomWindow(theWindow,partCode,FALSE);
  102.          InvalRect(&theWindow->portRect);
  103.          DrawGrowIcon(theWindow);
  104.          SetPort(oldPort);
  105.         }
  106.         break;
  107.       }
  108.       return(TRUE);
  109. }
  110.  
  111. static void Update(void)
  112. {
  113.     GrafPtr oldPort;
  114.     GetPort(&oldPort);
  115.     SetPort(myWindow);
  116.     BeginUpdate(myWindow);
  117.     EraseRect(&myWindow->portRect);
  118.     DrawPlot();
  119.     DrawGrowIcon(myWindow);
  120.     EndUpdate(myWindow);
  121.     checkcursor();    /* any change in window size generates an update */
  122.     SetPort(oldPort);
  123. }
  124.  
  125. static void activate(void)
  126. {
  127.    SelectWindow(myWindow);
  128.    DrawGrowIcon(myWindow);
  129. }
  130.  
  131. static void deactivate(void)
  132. {
  133.    DrawGrowIcon(myWindow);
  134. }
  135.  
  136. /* handle messages from MathPad */
  137. static void MPmessage(long msg)
  138. {
  139.   /*
  140.    switch(msg)
  141.    {
  142.     case DOCSAVED:
  143.      break;
  144.  
  145.     case DOCCLOSE:
  146.      break;
  147.      ...
  148.    }
  149.    */
  150. }
  151.  
  152. short doevent(EventRecord *theEvent)
  153. {
  154.     switch (theEvent->what)
  155.     {
  156.      case mouseDown:
  157.       return DoMouseDown(theEvent);
  158.       
  159.      case updateEvt:
  160.       if(myWindow == (WindowPtr)(theEvent->message))
  161.       {
  162.        Update();
  163.        return(TRUE);
  164.       }
  165.       break;
  166.       
  167.      case activateEvt:
  168.       if(myWindow == (WindowPtr)(theEvent->message))
  169.       {
  170.        if(theEvent->modifiers & activeFlag)
  171.         activate();
  172.        else
  173.         deactivate();
  174.        return(TRUE);
  175.       }
  176.       break;
  177.  
  178.      case app3Evt:
  179.       MPmessage(theEvent->message);
  180.       break;
  181.       
  182.      case app4Evt:
  183.       if((theEvent->message & 0xFF000000) == 0x01000000)
  184.       {
  185.           if(theEvent->message & 1)    /* resume */
  186.           {
  187.            if(myWindow == FrontWindow()) activate();
  188.          }
  189.           else                         /* suspend */
  190.           {
  191.            deactivate();
  192.          }
  193.          return(FALSE);    /* MathPad needs to do further processing  */
  194.       }
  195.       else
  196.           if((theEvent->message & 0xFF000000) == 0xFA000000)    /* mouse moved */
  197.             return(checkcursor());
  198.       break;
  199.  
  200.     }
  201.     return(FALSE);
  202. }
  203.  
  204.